package net.entelijan.cobean.tree;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.AbstractButton;
import javax.swing.JTree;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.MutableTreeNode;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;

import net.entelijan.cobean.util.CobeanUtil;

public class AbstractTreeInitializer {

	private AbstractButton coCollapseAllButton;

	private AbstractButton coExpandAllButton;

	protected JTree coTree;

	protected int rowHeight;

	protected boolean rootVisible = true;

	protected IPathFinder pathFinder = createDefaultPathFinder();

	public AbstractTreeInitializer() {
		super();
	}

	private IPathFinder createDefaultPathFinder() {
		return (IPathFinder) CobeanUtil
				.createInstance("net.entelijan.cobean.tree.pathfind.RecursivePathFinder");
	}

	protected void commonInit() {
		if (rowHeight > 0) {
			coTree.setRowHeight(rowHeight);
		}
		coTree.setRootVisible(this.rootVisible);
		if (coExpandAllButton != null) {
			coExpandAllButton.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent e) {
					throw new IllegalStateException("NOT YET IMPL");
				}

			});

		}

		if (coCollapseAllButton != null) {
			coCollapseAllButton.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent e) {
					for (int i = coTree.getRowCount() - 1; i >= 0; i--) {
						coTree.collapseRow(i);
					}
					coTree.clearSelection();
				}

			});

		}
	}

	protected TreePath createTreePath(Object value) {
		CobeanTreeNode root = (CobeanTreeNode) coTree.getModel().getRoot();

		List<?> path = pathFinder.findPath(root, value);
		TreePath re;
		if (path == null) {
			re = new TreePath(new Object[0]);
		} else {
			re = new TreePath(path.toArray());
		}
		return re;
	}

	protected TreeModel createTreeModel(Object value, ITreeLevelDescription desc) {
		MutableTreeNode root = new CobeanTreeNode(value, desc);
		return new DefaultTreeModel(root);
	}

	public JTree getCoTree() {
		return coTree;
	}

	public void setCoTree(JTree coTree) {
		this.coTree = coTree;
	}

	public AbstractButton getCoCollapseAllButton() {
		return coCollapseAllButton;
	}

	public void setCoCollapseAllButton(AbstractButton coCollapseAllButton) {
		this.coCollapseAllButton = coCollapseAllButton;
	}

	public AbstractButton getCoExpandAllButton() {
		return coExpandAllButton;
	}

	public void setCoExpandAllButton(AbstractButton coExpandAllButton) {
		this.coExpandAllButton = coExpandAllButton;
	}

	public int getRowHeight() {
		return rowHeight;
	}

	public void setRowHeight(int rowHeight) {
		this.rowHeight = rowHeight;
	}

	public boolean isRootVisible() {
		return rootVisible;
	}

	public void setRootVisible(boolean rootVisible) {
		this.rootVisible = rootVisible;
	}

	public IPathFinder getPathFinder() {
		return pathFinder;
	}

	public void setPathFinder(IPathFinder pathFinder) {
		this.pathFinder = pathFinder;
	}

}
